package com.android.duininsuoai.utils;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import okhttp3.FormBody;

/**
 * http请求类
 * Created by wzq on 2016/9/1.
 */
public class HttpUtils {

    /**
     * POST请求
     * 有请求头
     *
//     * @param url
//     * @param mHeaders
//     * @param mParams
//     * @param mCallback
     */


    /**
     * POST请求
     * 无请求头
     *
     * @param url
     * @param mParams
     * @param mCallback
     */
    public static void post(String url,  Map<String, String> mHeaders,Map<String, String> mParams, Callback mCallback) {
        print(url,mHeaders,mParams,true);
        OkHttpUtils
                .post()
                .url(url)
                .headers(mHeaders)
                .params(mParams)
                .build()
                .execute(mCallback);
    }
    public static void post(String url, Map<String, String> mParams, Callback mCallback) {
        print(url,null,mParams,true);
        OkHttpUtils
                .post()
                .url(url)
                .params(mParams)
                .build()
                .execute(mCallback);
    }

    /**
     * GET请求
     * 有请求头
     *
     * @param url
     * @param mHeaders
     * @param mParams
     * @param mCallback
     */
    public static void get(String url, Map<String, String> mHeaders, Map<String, String> mParams, Callback mCallback) {
        print(url,mHeaders,mParams,false);

        OkHttpUtils
                .get()
                .url(url)
                .headers(mHeaders)
                .params(mParams)
                .build()
                .execute(mCallback);
    }


    /**
     * GET请求
     * 无请求头
     *
     * @param url
     * @param mParams
     * @param mCallback
     */
    public static void get(String url, Map<String, String> mParams, Callback mCallback) {
        print(url,null,mParams,false);
        OkHttpUtils
                .get()
                .url(url)
                .params(mParams)
                .build()
                .execute(mCallback);
    }

    /**
     * POST上传文件
     * 有请求头
     *
     * @param url
     * @param mHeaders
     * @param mCallback
     */
    public static void postfile(String url, Map<String, String> mHeaders, String fileKey, File file, Callback mCallback) {
        OkHttpUtils
                .post()
                .url(url)
                .headers(mHeaders)
                .addFile(fileKey,String.valueOf(System.currentTimeMillis()) + ".png",file)
                .build()
                .execute(mCallback);
    }

    public static void print(String url,final Map<String, String> mHeaders, final Map<String ,String> params,boolean HTTP_TYPE){
        StringBuffer queryString=new StringBuffer();
        if(params != null){

            FormBody.Builder pairs=new FormBody.Builder();

            Set<String> keys = params.keySet();
            for(Iterator<String> i = keys.iterator(); i.hasNext();) {
                String key = (String)i.next();
                pairs.add(key, params.get(key));

                queryString.append(key+"=");
                queryString.append(params.get(key)+"&");
            }

        }

        StringBuffer mHeadersString=new StringBuffer();
        if(mHeaders != null){

            FormBody.Builder mHeaderpairs=new FormBody.Builder();
            Set<String> keys = mHeaders.keySet();
            for(Iterator<String> i = keys.iterator(); i.hasNext();) {
                String key = (String)i.next();
                mHeaderpairs.add(key, mHeaders.get(key));

                mHeadersString.append(key+"=");
                mHeadersString.append(mHeaders.get(key)+"&");
            }

        }

        if(HTTP_TYPE) {
            ReleaseManager.printLog("HTTP_POST", url + "  post: " + queryString.toString() +" header: "+mHeadersString);
        }else {
            ReleaseManager.printLog("HTTP_GET", url + "?" + queryString.toString()+" header: "+mHeadersString);
        }

    }


}
